<div class="container-fluid">
    <h4 class="content-head">系统参数</h4>
    <div class="table-wrapper users-table section">
        <div class="input-group" id="search">
            &nbsp;<label for="type">类型：</label>
            <input type="text" name="type" class="input-small" id="type">
       <span class="btn_area">
           <button id="search_btn" type="button" class="btn btn-purple btn-sm">
               搜索
               <i class="icon-search icon-on-right bigger-110"/>
           </button>
       </span>
        </div>
        <div id="list"></div>
    </div>
</div>

<script>
    (function () {
        var url = 'sysConfig/list-data';
        var $datagrid = $('#list');

        loadData();

        $('#search_btn').on('click', function () {
            loadData();
        });

        function loadData() {
            $datagrid.datagrid({
                action: true,
                doUpdate: function (rowData) {
                    doUpateData(rowData.id);
                },
                doDel: function (rowData) {
                    doDelete(rowData.id);
                },
                'toolbar': [{
                    type: 'ADD',
                    click: function () {
                        addSysConfig();
                    }
                }],
                'dataModel': {
                    'url': url,
                    'parameters': getCommJsonParams('search')
                },
                'columns': [
                    {title: '名称', dataField: 'title'},
                    {title: '类型', dataField: 'type'},
                    {title: '值', dataField: 'value'},
                    {title: '状态', dataField: 'stateDict'},
                    {title: '排序', dataField: 'sno'},
                    {title: '备注', dataField: 'remark'}
                ]
            });
        };


        function doUpateData(id) {
            var $div = $('<div>');
            $div.load('sysConfig/form?id=' + id, function (response, status, xhr) {
                if (xhr.status == 200) {
                    layer.open({
                        type: 1,//Page层类型
                        area: ['400px', '520px'],
                        title: '修改参数信息',
                        btn: ['提交修改'],
                        btnAlign: 'c',
                        content: $div.html(),
                        yes: function (layId) {
                            var param = $('#sysConfigForm').serialize();
                            $.post('sysConfig/update', param, function (result) {
                                if (result.success) {
                                    loadData();
                                    layer.msg('修改成功', {icon: 1});
                                    layer.close(layId)
                                } else {
                                    layer.msg('修改失败', {icon: 5});
                                }
                            }, 'json')
                        }
                    });
                }
            });
        }
        function doDelete(id) {
            layer.confirm('确定删除此记录',function () {
                 $.post('sysConfig/delete',{'id':id},function (result) {
                      if(result.success){
                          loadData();
                          layer.msg('删除成功', {icon: 1});
                      }else{
                          layer.msg('删除成功', {icon: 5});
                      }
                 },'json');
            })
        }
        function addSysConfig() {
            var $div = $('<div>');
            $div.load('sysConfig/form', function (response, status, xhr) {
                if (xhr.status == 200) {
                    layer.open({
                        type: 1,//Page层类型
                        area: ['400px', '500px'],
                        title: '新增参数',
                        btn: ['提交'],
                        btnAlign: 'c',
                        content: $div.html(),
                        yes: function (layId) {
                            var param = $('#sysConfigForm').serialize();
                            $.post('sysConfig/add', param, function (result) {
                                if (result.success) {
                                    loadData();
                                    layer.msg('新增成功', {icon: 1});
                                    layer.close(layId)
                                } else {
                                    layer.msg(result.message, {icon: 5});
                                }
                            }, 'json')
                        }
                    });
                }
            });
        }
    })();
</script>
